#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h> /* for _getch() */

extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
	static int l_cppfunction(lua_State *L) {
		double arg = luaL_checknumber(L, 1);
		lua_pushnumber(L, arg * 0.5);
		return 1;
	}
}

using namespace std;

lua_State *L = lua_open();
void listAllFunction();
void inputFunction();
void eraseAllFunction();
void totalFunction();
int s;

int main()
{
	
	int userSelection = -1;


	cout << "	*****************************************************\n";
	cout << "	*               WELCOME TO E-REPROT                 *\n";
	cout << "	*                                                   *\n";
	cout << "	*  E-Report is a simple program designed to help    *\n";
	cout << "	*  the user store, calculate, and view expenses.    *\n";
	cout << "	*                                                   *\n";
	cout << "	*****************************************************\n";
	cout << "	press enter to continue" << endl;
	_getch();
	system("CLS");

	while (userSelection != 5)
	{
		system("CLS");
		cout << "	*****************************************************\n";
		cout << "	*                                                   *\n";
		cout << "	* Please select from one of the following options   *\n";
		cout << "	* 1. View expense report                            *\n";
		cout << "	* 2. Add expense                                    *\n";
		cout << "	* 3. View total of expenses                         *\n";
		cout << "	* 4. Delete all records                             *\n";
		cout << "	* 5. Exit                                           *\n";
		cout << "	*                                                   *\n";
		cout << "	*****************************************************" << endl;
		while ((cout << "Option: ")
			&& (!(cin >> userSelection) || userSelection < 1 || userSelection > 5)) {
			cout << "Error... Please enter a number between 1 and 5 \n";
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
		}
		switch (userSelection)
		{
		case 1:
			listAllFunction();
			break;
		case 2:
			inputFunction();
			break;
		case 3:
			totalFunction();
			break;
		case 4:
			eraseAllFunction();
			break;
		case 5:
			cout << "\n";
			break;
		default:
			cout << "Error, bad input, quitting\n";
			break;
		}
	}
	lua_close(L);
	return 0;
}
	void listAllFunction() {
		luaL_openlibs(L); // load the libs
		s = luaL_dofile(L, "luascript.lua"); //run a Lua scrip here
		lua_getglobal(L, "myDisplayAllfunction");
		lua_pcall(L, 0, 0, 0);
		return;
}
	void inputFunction() {
		luaL_openlibs(L); // load the libs
		s = luaL_dofile(L, "luascript.lua"); //run a Lua scrip here
		lua_getglobal(L, "myAddfunction");
		lua_pcall(L, 0, 0, 0);
		return;
	}
	void eraseAllFunction() {
		luaL_openlibs(L); // load the libs
		s = luaL_dofile(L, "luascript.lua"); //run a Lua scrip here
		lua_getglobal(L, "myErasefunction");
		lua_pcall(L, 0, 0, 0);
		return;
	}
	void totalFunction() {
		luaL_openlibs(L); // load the libs
		s = luaL_dofile(L, "luascript.lua"); //run a Lua scrip here
		lua_getglobal(L, "expenseTotal");
		lua_pcall(L, 0, 0, 0);
		return;
	}